
package com.hlkj.pay.controller.user;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.provider.Aes256EncryptionProvider;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.user.IUserAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.user.UserQueryDto;
import com.hlkj.pay.dto.user.UserTeleChargeOrderQueryDto;
import com.hlkj.pay.infrastructure.model.user.UserDO;
import com.hlkj.pay.infrastructure.model.user.UserTeleChargeOrderDO;
import com.hlkj.pay.service.user.IUserRechargeService;
import com.hlkj.pay.service.user.IUserService;
import com.hlkj.pay.vo.common.req.CommonUpdateStatusReq;
import com.hlkj.pay.vo.user.req.UserQueryPageReq;
import com.hlkj.pay.vo.user.req.UserTeleChargeOrderQueryPageReq;
import com.hlkj.pay.vo.user.req.UserTeleChargeOrderReq;
import com.hlkj.pay.vo.user.resp.UserResp;
import com.hlkj.pay.vo.user.resp.UserTeleChargeOrderResp;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/25 14:16
 */
@Tag(name = "C端用户 后台查询")
@RestController
@RequestMapping("/v1/user/admin")
@Slf4j
@RequiredArgsConstructor
public class UserAdminController extends BaseController {

    private final IUserAppService userAppService;

    private final IUserService userService;

    private final IUserRechargeService userRechargeService;

    private final Aes256EncryptionProvider aes256EncryptionProvider;

    @Operation(summary = "用户列表")
    @GetMapping("/page")
    @ResponseBody
    CommonResult<List<UserResp>> userList(UserQueryPageReq userQueryPageReq) {
        UserQueryDto userQueryDto = BeanUtil.copyProperties(userQueryPageReq, UserQueryDto.class);
        PageResult<UserDO> page = userService.queryPage(userQueryDto);
        List<UserDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        list.stream().forEach(userDO -> {
            try {
                userDO.setPhone(aes256EncryptionProvider.decryptString(userDO.getPhone()));
            }
            catch (Exception e) {
            }
        });
        return buildPageResult(page, BeanUtil.copyToList(list, UserResp.class));
    }

    @Operation(summary = "更新用户状态")
    @GetMapping("/status")
    @ResponseBody
    CommonResult<Void> updateUserStatus(@Validated @RequestBody CommonUpdateStatusReq commonUpdateStatusReq) {
        return userAppService.updateUserStatus(commonUpdateStatusReq);
    }

    @Operation(summary = "用户充值列表")
    @GetMapping("/charge/page")
    @ResponseBody
    CommonResult<PageResult<UserTeleChargeOrderResp>> chargeList(UserTeleChargeOrderQueryPageReq userTeleChargeOrderQueryPageReq) {
        UserTeleChargeOrderQueryDto userTeleChargeOrderQueryDto = BeanUtil.copyProperties(userTeleChargeOrderQueryPageReq, UserTeleChargeOrderQueryDto.class);
        processQueryDate(userTeleChargeOrderQueryPageReq, userTeleChargeOrderQueryDto);
        PageResult<UserTeleChargeOrderDO> page = userRechargeService.queryPage(userTeleChargeOrderQueryDto);
        List<UserTeleChargeOrderDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, BeanUtil.copyToList(list, UserTeleChargeOrderResp.class));
    }

    @Operation(summary = "更新话费充值状态")
    @PostMapping("/charge/status")
    @ResponseBody
    CommonResult<Void> updateRechargeStatus(@Validated @RequestBody UserTeleChargeOrderReq userTeleChargeOrderReq) {
        return userAppService.updateRechargeStatus(userTeleChargeOrderReq);
    }
}
